﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Transport
{
    class Client
    {
        private IPAddress ip;
        private int port;
        private StreamWriter swSender;
        private TcpClient tcpClient;
        private Thread thrMessaging;

        public Client(IPAddress ip, int port)
        {
            this.ip = ip;
            this.port = port;
        }

        public bool Connect()
        {
            try
            {
                tcpClient = new TcpClient();
                tcpClient.Connect(ip, port);
                swSender = new StreamWriter(tcpClient.GetStream());
            }
            catch (Exception)
            {
                MessageBox.Show("The client can not connect");
                return false;
            }

            return true;
        }

        public void SendKey(string keys)
        {
            swSender.WriteLine(keys);
            swSender.Flush();
            
        }

        public void CloseConnection()
        {
            swSender.Close();
            tcpClient.Close();
        }
    }
}
